home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Floating point calculation order
- Date: 26 Jan 1996 15:17:44 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan26081744@qcd.lanl.gov>
- References: <m0tedv8-0002eqC@sice.nsk.su> <31083426.197A@imsisoft.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Victor Bazarov's message of Thu, 25 Jan 1996 17:53:42 -0800
-
- --text follows this line--
- In article <31083426.197A@imsisoft.com> Victor Bazarov
- <vbazarov@imsisoft.com> writes:
- <snip>
- > double x, p, q, r;
- >
- > my compiler (cc on SCO 3.2) calculated following expression:
- >
- > x = p * q / r;
- >
- > as p * (q/r); (I mean, it divided first, than multiplied). That
- > resulted in precision loss.
- >
- > Is this legal behavior?
-
- According to ISO/IEC 9899, it is implementation defined, I guess.
-
- I disagree.
-
-
- On the page 7 the behaviour of the abstract machine is described:
-
- "In the abstract machine, all expressions are evaluated as specified
- by the semantics. An actual implementation need not evaluate part
- of an expression if it can deduce that its value is not used and
- that no needed side effects are produced (including any caused by
- calling a function or accessing a volatile object)."
-
- From the wording it is not immediately clear if compiler is allowed
- to rewrite the expression. But in the example on the page 9 one can
-
- I would say it is absolutely clear. The quoted clause does not allow
- reordering: nothing else allows it either if there is a difference in
- meaning. If it is not allowed by any clause, it is disallowed by last
- requirement of 5.1.2.3.
-
- conclude that the expression a + 32760 + b + 5 can be rewritten as
- ((a + 32765) + b) on a machine without exceptions on integer overflow.
-
- You are misinterpreting the purpose of the example. This is
- illustrating the `as if' rule: the rule at the end of 5.1.2.3 (guess
- top of page 8). The reordering is allowed because in a machine without
- integer overflow, the effects of a + 32760 + b + 5 are exactly the
- same as those of a + 32765 + b.
-
- In the case under discussion, the poster specifically mentioned (by
- using the words `precision loss') that there was a noticeable
- difference between (p*q)/r and p*(q/r). Given that a difference
- exists, the last part (beginning of page 8) of 5.1.2.3 disallows the
- reordering.
-
- So, in any case compiler vendor could say: 'Hey, I'm trying to help
- you here, avoiding overflow from multiplication.', although at the
- closer look, division can produce similar overflow...
-
- A compiler vendor can say whatever it likes as long as it does not
- claim that the compiler is ANSI conformant.
-
- I few words, put the braces the way you want the expression to be
- evaluated, and complain if compiler does not obey them.
-
- Why should a compiler `obey braces'? The braces in (p * q)/r do not do
- _anything_ to the meaning of the program.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-